//
//  SecondViewController.m
//  UI10_homework
//
//  Created by aaaaaa on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "CustomLableTextFieldView.h"

@interface SecondViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, retain) UIButton *headerButton;
@property (nonatomic, retain) NSMutableArray<CustomLableTextFieldView *> *ltViewArray;

@end

@implementation SecondViewController

- (void)dealloc
{
    [_indexPath release];
    [_headerButton release];
    [_dictionary release];
    [_ltViewArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self createHeader];
    [self createLTView];
    [self createEditButton];
    [self createPreservationButton];
}


- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = [self.dictionary objectForKey:@"name"];
    

    self.ltViewArray = [[NSMutableArray alloc] init];
}

//头像按钮
- (void)createHeader{
    self.headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerButton setImage:[UIImage imageNamed:[self.dictionary objectForKey:@"header"]] forState:UIControlStateNormal];
    self.headerButton.frame = CGRectMake(167, 80, 100, 100);
    
    self.headerButton.layer.cornerRadius = self.headerButton.bounds.size.width / 2;
    self.headerButton.layer.masksToBounds = YES;
    
    [self.view addSubview:self.headerButton];
    
    [self.headerButton addTarget:self action:@selector(headerAction:) forControlEvents:UIControlEventTouchUpInside];
}

//头像按钮点击事件
-(void)headerAction:(UIButton *)button{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{
        
        
    }];
    [picker release];
}

//实现UIImagePickerController协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.headerButton setImage:image forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

//信息
- (void)createLTView{
    NSArray *keys = [self.dictionary allKeys];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(57, 260, 300, 300)];
    int j = 0;
    for (int i = 0; i < keys.count ; i++) {
        if ([keys[i] isEqualToString:@"header"]) {
            j++;
            continue;
        }
        
        CustomLableTextFieldView *ltView = [[CustomLableTextFieldView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(view.bounds) / (keys.count - 1) * (i - j),CGRectGetWidth(view.bounds) , 40)];
        
        ltView.lable.text = keys[i];
        ltView.textField.placeholder = [self.dictionary objectForKey:keys[i]];
        [view addSubview:ltView];
        [ltView release];
        
        //当key值为QQ或者number是弹出数字键盘
        if ([keys[i] isEqualToString:@"QQ"] || [keys[i] isEqualToString:@"number"]) {
            ltView.textField.keyboardType = UIKeyboardTypeNumberPad;
        }
        [self.ltViewArray addObject:ltView];
    }
    [self.view addSubview:view];
    [view release];
}

//修改按钮
- (void)createEditButton{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction:)];
}

//修改按钮点击事件 点击允许用户修改
- (void)editAction:(UIBarButtonItem *)bar{
    for (CustomLableTextFieldView *view in self.ltViewArray) {
        view.textField.userInteractionEnabled = YES;
        view.textField.text = view.textField.placeholder;
    }
}

//保存按钮
- (void)createPreservationButton{
    
    UIButton *savebutton = [UIButton buttonWithType:UIButtonTypeSystem];
    savebutton.frame = CGRectMake(172, 600, 80, 30);
    [savebutton addTarget:self action:@selector(preservationAction:) forControlEvents:UIControlEventTouchUpInside];
    [savebutton setTitle:@"Save" forState:UIControlStateNormal];
    [self.view addSubview:savebutton];
}

//保存按钮点击事件
- (void)preservationAction:(UIButton *)button{
    NSArray *array = [self.dictionary allKeys];
    NSMutableDictionary *dic = self.dictionary.mutableCopy;
    int k = 0;
    for (int i = 0; i < array.count ; i++) {
        if ([array[i] isEqualToString:@"header"]) {
            k++;
            continue;
        }
        [dic setObject:self.ltViewArray[i - k].textField.text forKey:array[i]];
    }
    [self.delegate preservation:dic withIndexPath:self.indexPath];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

//点击屏幕任意一点回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    for (CustomLableTextFieldView *view in self.ltViewArray) {
        [view.textField resignFirstResponder];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
